﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media.Media3D;

using _3DTools;

namespace SBPweb.MediaCenter.Tools.WPF.Converters
{
    [ValueConversion(typeof(Camera), typeof(ScaleTransform3D))]
    public class CamToScaleConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            PerspectiveCamera p = values[0] as PerspectiveCamera;

            if (p != null && values[1] is double && values[2] is double)
            {
                double width = (double)values[1];
                double height = (double)values[2];

                double aspectRatio = width / height;

                double hFoV = MathUtils.DegreesToRadians(p.FieldOfView) / 2;
                double xScale = Math.Tan(hFoV) * p.Position.Z;
                double yScale = 1 / aspectRatio * xScale;
                return new ScaleTransform3D(xScale, yScale, 1.0);
            }

            return new ScaleTransform3D(1, 1, 1);
        }

        public object[] ConvertBack(object values, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}